package atom.message 
{
	
	import atom.error.AtomError;
	import com.adobe.serialization.json.JSON;
	import flash.events.Event;
	
	/**
	 * Represents a model for messages.
	 * @author Jorge Braccini
	 */
	public class Message {
		
		private static var seed:int = 0;
		
		private var _type:String;
		private var _id:int;
		private var _data:Object;
		
		public function Message(type:String = "") {
			_id = Message.seed;
			_type = type;
			
			Message.seed++;
		}
		
		public function parse(message:String):void {
			try {
				var o:Object = JSON.decode(message);
			} catch (e:Error) {
				throw new AtomError(AtomError.SOCKET_PARSE_ERROR);
			}
			
			_type 	= o.type;
			_data 	= o.data;
			_id 	= o.id;
		}
		
		public function toJSON():String {
			var o:Object = {
				type:		type,
				id:			id,
				data:		data
			}
			
			return JSON.encode(o);
		}
		
		//////////////////////////////////////////////////////////
		// Properties ////////////////////////////////////////////
		//////////////////////////////////////////////////////////
		
		public function get data():Object { return _data; }
		
		public function set data(value:Object):void {
			_data = value;
		}
		
		public function get type():String { return _type; }
		
		public function set type(value:String):void {
			_type = value;
		}
		
		public function get id():int { return _id; }
		
	}

}